package cn.com.gridinfo.sooc.core.login.activity.forgetpwd;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.SoocApplication;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.login.bean.MiBaoQuestion;
import cn.com.gridinfo.sooc.core.login.common.CodeConst;
import cn.com.gridinfo.sooc.core.login.dao.ForgetDao;
import cn.com.gridinfo.sooc.core.my.setting.activity.MiBaoActivity;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 密保找回
 * Created by 刘晓晨 on 2015/12/3.
 */
public class BackPwdMiBaoActivity extends BaseSoocActivity {
    @InjectView(R.id.question1)//问题1
            TextView question1;
    @InjectView(R.id.answer1)//回答1
            ClearEditText answer1;
    @InjectView(R.id.question2)//问题2
            TextView question2;
    @InjectView(R.id.answer2)//回答2
            ClearEditText answer2;
    @InjectView(R.id.question3)//问题3
            TextView question3;
    @InjectView(R.id.answer3)//回答3
            ClearEditText answer3;
    @InjectView(R.id.send)//提交
            Button send;
    private String xtdz = ""; //获取节点域名
    private String uid = "";//获得UID
    private ForgetDao dao;//用户dao
    private String id1, id2, id3;// 定义问题id
    private String result1, result2, result3;//全局变量：回答1,2,3
    private String type = "pro";//找回方式
    private String way;//从那个地方调用的这个程序 000：从设置中 111：从忘记密码中
    List<MiBaoQuestion> list;//密保问题实体类
    String title;//标题

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.back_pwd_mibao_activity);
        SoocApplication.getContext().addActivity(this);
        ButterKnife.inject(this);
        dao = new ForgetDao(this);
        title = "密保问题";
        way = getIntent().getStringExtra("way");//从哪个地方调用的这个程序 000：从设置中 111：从忘记密码中
        if (way.equals("000")) {
            title = "验证密保问题";
            send.setText("提交");
        }
        xtdz = getIntent().getStringExtra("xtdz");//节点
        uid = getIntent().getStringExtra("uid");//uid

        dao.setXtdz(xtdz);
        initQuestion();//请求接口
        send.setEnabled(false);//按钮默认不可点击
        answer3.setImeOptions(EditorInfo.IME_ACTION_GO);
        answer3.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    if (validateNotNull()) {
                        send();
                    }

                }
                return false;
            }
        });
    }

    private boolean validateNotNull() {
        //问题一
        if (StringUtils.isNull(answer1.getText().toString().trim())) {
            answer1.requestFocus();
            return false;
        }
        //问题二
        if (StringUtils.isNull(answer2.getText().toString().trim())) {
            answer2.requestFocus();
            return false;
        }
        //问题三
        if (StringUtils.isNull(answer3.getText().toString().trim())) {
            answer3.requestFocus();
            return false;
        }
        return true;
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return title;
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //返回
    @OnClick(R.id.toolbar_leftbtn)
    void onGoBack() {
        finish();
        //返回数据，3个问题，对应的ID
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        switch (requestCode) {
            case CodeConst.MIBAOWENTI:
                if (dao.status.equals("0")) {//请求成功
                    //设置第一个密保问题，拿到id
                    question1.setText("问题一: "+dao.miBaoQuestion.get(0).getMbwt());
                    id1 = dao.miBaoQuestion.get(0).getId();
                    Log.d("第一个问题的ID " + id1);
                    //设置第二个密保问题，拿到id
                    question2.setText("问题二: "+dao.miBaoQuestion.get(1).getMbwt());
                    id2 = dao.miBaoQuestion.get(1).getId();
                    //设置第三个密保问题，拿到id
                    question3.setText("问题三: "+dao.miBaoQuestion.get(2).getMbwt());
                    id3 = dao.miBaoQuestion.get(2).getId();
                }
                break;
            case CodeConst.SUERMIBAO:
                if (dao.status.equals("0")) {
                    if (way.equals("111")) {//标志从哪儿进入这个activity  111： 从找回密码进入
                        Intent i = new Intent(this, ResetPwdActivity.class);
                        i.putExtra("xtdz", xtdz);//将节点域名传出
                        i.putExtra("uid", uid);//将uid传出
                        i.putExtra("type", type);// 将找回方式传出 （密保找回）
                        startActivity(i);
                        finish();
                        AnimUtil.intentSlidIn(this);
                    } else if (way.equals("000")) {//000： 从修改密保问题验证进入
                        startActivity(new Intent(this, MiBaoActivity.class));
                        setResult(RESULT_OK);
                        finish();
                        AnimUtil.intentSlidIn(this);//动画
                    }
                } else {
                    MessageUtils.showImageShortToast(this, "密保问题验证失败");
                }
                break;
        }
    }

    @Override
    public void onRequestFaild(int requestCode, String errorNo, String errorMessage) {
        super.onRequestFaild(requestCode, errorNo, errorMessage);
        MessageUtils.showImageShortToast(this, dao.getStatusCode());
    }

    //初始化3个问题
    private void initQuestion() {
        dao.mibaoQuestion(uid, uid + "0S3FV7PH8MLJT6IOAG2ECWQNXUBR1YZ459KD");
    }

    //点击提交，发送请求  问题是否正确
    @OnClick(R.id.send)
    void send() {
        result1 = answer1.getText().toString().trim();// 获取3个问题的答案
        result2 = answer2.getText().toString().trim();
        result3 = answer3.getText().toString().trim();
        //拼接字符串 请求接口
        String data = "{\"" + id1 + "\":\"" + result1 + "\",\"" + id2 + "\":\"" + result2 + "\",\"" + id3 + "\":\"" + result3 + "\"}";
        dao.mibaoSure(uid, data, uid + data + "0S3FV7PH8MLJT6IOAG2ECWQNXUBR1YZ459KD");
    }

    //监听输入框都有内容才可以点击按钮
    @OnTextChanged(value = {R.id.answer1, R.id.answer2, R.id.answer3}, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void OnTextChanged() {
        if (StringUtils.isNull(answer1.getText().toString()) || StringUtils.isNull(answer2.getText().toString())
                || StringUtils.isNull(answer3.getText().toString())) {
            send.setEnabled(false);
        } else {
            send.setEnabled(true);
        }
    }

}
